<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'信息资料列表'),
	array('op'=>'edit','title'=>'添加信息资料'),	
);
$return['groups']=pdo_getall('jy_yiliao_pdf_group',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('title','id'),'id');

if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['merchid'] = $merchid;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_yiliao_pdf',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'信息资料标题','type'=>'string'),
			array('name'=>'thumb','title'=>'图片','type'=>'image'),
			array('name'=>'group_id','title'=>'所属分类','type'=>'select','options'=>'groups'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yiliao_pdf',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回信息资料列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'信息资料标题','type'=>'string'),
			array('name'=>'desc','title'=>'描述','type'=>'string'),
			array('name'=>'thumb','title'=>'图片','type'=>'image'),
			array('name'=>'group_id','title'=>'所属分类','type'=>'select','options'=>'groups'),
			array('name'=>'pdf','title'=>'上传PDF资料','type'=>'upfile'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写名称','error');
	}
	$postdata['merchid'] = $merchid;
	$data=array_fields(array('title','desc','thumb','group_id','status','pdf','merchid'),$postdata);
	$id=intval($postdata['id']);
	
	if($id){
		pdo_update('jy_yiliao_pdf',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_yiliao_pdf',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_yiliao_pdf',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的信息资料！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_yiliao_pdf',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}